package com.zxy.auth.findhouse.com.controller;

import java.util.Arrays;
import java.util.Map;


import com.zxy.auth.findhouse.com.config.JwtUtil;
import com.zxy.auth.findhouse.com.config.MD5Util;
import com.zxy.auth.findhouse.com.entity.AdminEntity;
import com.zxy.auth.findhouse.com.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import com.zxy.common.utils.PageUtils;
import com.zxy.common.utils.R;



/**
 * 
 *
 * @author zxy
 * @email 1695483548@qq.com
 * @date 2022-04-22 15:02:48
 */
@RestController
@RequestMapping("auth.findhouse.com/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = adminService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{username}")
    public R info(@PathVariable("username") String username){
		AdminEntity admin = adminService.getById(username);

        return R.ok().put("admin", admin);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AdminEntity admin){
		adminService.save(admin);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AdminEntity admin){
		adminService.updateById(admin);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] usernames){
		adminService.removeByIds(Arrays.asList(usernames));

        return R.ok();
    }

    /*
     * 超级管理员登录
     * */
    @RequestMapping("/admin")
    public R admin(@RequestBody AdminEntity admin){
        System.out.println(jwtUtil);
        AdminEntity adminEntity = adminService.getById(admin.getUsername());
        if (adminEntity!=null){
            if (adminEntity.getPwd().equals(MD5Util.md5Code(admin.getPwd(),2))){
                if (adminEntity.getStatus().equals(0)){
                    String token = jwtUtil.createJWT(admin.getUsername(),admin.getUsername(),"admin");
                    return R.ok().put("token",token).put("loginName",adminEntity.getUsername());
                }
            }
        }
        return R.error(500,"error~~~~~~~~~~~");
    }

    /*
     * 站点管路员登录
     * */
    @RequestMapping("/pointadmin")
    public R pointadmin(@RequestBody AdminEntity admin){
        System.out.println(jwtUtil);
        AdminEntity adminEntity = adminService.getById(admin.getUsername());
        if (adminEntity!=null){
            if (adminEntity.getPwd().equals(MD5Util.md5Code(admin.getPwd(),2))){
                if (adminEntity.getStatus().equals(1)){
                    String token = jwtUtil.createJWT(admin.getUsername(),admin.getUsername(),"admin");
                    return R.ok().put("token",token).put("loginName",adminEntity.getUsername());
                }
            }
        }
        return R.error(500,"error~~~~~~~~~~~");
    }

    /*
     * 经理登录
     * */
    @RequestMapping("/manager")
    public R manager(@RequestBody AdminEntity admin){
        System.out.println(jwtUtil);
        AdminEntity adminEntity = adminService.getById(admin.getUsername());
        if (adminEntity!=null){
            if (adminEntity.getPwd().equals(MD5Util.md5Code(admin.getPwd(),2))){
                if (adminEntity.getStatus().equals(2)){
                    String token = jwtUtil.createJWT(admin.getUsername(),admin.getUsername(),"admin");
                    return R.ok().put("token",token).put("loginName",adminEntity.getUsername());
                }
            }
        }
        return R.error(500,"error~~~~~~~~~~~");
    }

    /*
     * 用户登录
     * */
    @RequestMapping("/userlogin")
    public R userlogin(@RequestBody AdminEntity admin){
        System.out.println(jwtUtil);
        AdminEntity adminEntity = adminService.getById(admin.getUsername());
        if (adminEntity!=null){
            if (adminEntity.getPwd().equals(MD5Util.md5Code(admin.getPwd(),2))){
                if (adminEntity.getStatus().equals(3)){
                    String token = jwtUtil.createJWT(admin.getUsername(),admin.getUsername(),"admin");
                    return R.ok().put("token",token).put("loginName",adminEntity.getUsername());
                }
            }
        }
        return R.error(500,"error~~~~~~~~~~~");
    }

    //注册
    @PostMapping("/register")
    public R zhu(@RequestBody AdminEntity admin){
        //密码加密
        admin.setPwd(MD5Util.md5Code(admin.getPwd(),2));
        adminService.save(admin);
        return R.ok();
    }


    /*
    * 发短信
    * */
    @RequestMapping(value = "/sendSms/{phone}",method = RequestMethod.POST)
    public R sendSms(@PathVariable String phone) throws Exception {
        adminService.sendSms(phone);
        System.out.println("1");
        return R.ok();
    }




}
